package com.edu.juc.introduction.tickets.lock;

import java.util.concurrent.locks.ReentrantLock;

public class Tickets {
    public static Integer TICKETS = 30;

    // 创建可重入锁
    // 非公平锁/公平锁
    private final ReentrantLock lock = new ReentrantLock(true);

    public void sell() {
        lock.lock();
        try {
            if (TICKETS > 0) {
                System.out.println(Thread.currentThread().getName() + "正在出售第" + TICKETS + "张票");
                TICKETS--;
            } else {
                System.out.println(Thread.currentThread().getName() + "票已售完");
            }
        } finally {
            lock.unlock();
        }
    }
}
